<template>
  <div>
    <div>
      <label>用户名:</label><BaseInput v-model.trim="mobile" ref="useIpt" />
    </div>
    <div>
      <label>密码:</label
      ><BaseInput
        @keyup.native.enter="doLogin"
        v-model.trim="pwd"
        type="password"
      />
    </div>
    <div class="bottom-box">
      <BaseCheckBox v-model="isRem">记住账号</BaseCheckBox>
      <div class="right-content">
        <BaseButton @click="doLogin" type="success">登录</BaseButton>
      </div>
    </div>
  </div>
</template>

<script>
// 导入axios
import axios from "axios";
// 导入工具
import { setToken } from '@/utils/token'

import { loginAPI } from '@/api'

export default {
  name: "LoginBox",
  data() {
    return {
      isRem: false,
      mobile: "18888888888",
      pwd: "246810",
    };
  },
  methods: {
    getFocus() {
      this.$refs.useIpt.focus();
    },

    async doLogin(e) {

      // 使用async加await
      try {
        let res = await loginAPI({
          mobile: this.mobile,
          code: this.pwd
        })

        // 登录成功，弹出提示
        alert('登录成功！')
        // 保存token
        setToken(res.data.data.token)
        // 跳转
        this.$router.push('/layout/home')

      } catch (err) {

        alert('账号或密码错误')
      }
    },
  },
  mounted() {
    this.getFocus();
  },
  activated() {
    this.getFocus();
  },
};
</script>

<style scoped lang="less">
.bottom-box {
  display: flex;
  justify-content: space-between;
  padding: 0 40px;
  padding-left: 90px;
}
.login-title {
  color: #252526;
  font-weight: normal;
}

label {
  display: inline-block;
  width: 60px;
  text-align: right;
}
</style>
